package com.brand.prisd.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.brand.prisd.R;
import com.brand.prisd.ViewHolder.Type7ViewHolder;
import com.brand.prisd.activity.ChooseDeviceTypeActivity;
import com.brand.prisd.sp.MySharedPreferences;
import com.orhanobut.logger.Logger;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.IDevListener;
import com.tuya.smart.sdk.api.ITuyaDevice;
import com.tuya.smart.sdk.bean.DeviceBean;

import static com.brand.prisd.bean.VariableStorage.ITEM_VIEW_TYPE_7;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddDeviceFragment extends Fragment implements View.OnClickListener, IDevListener {

    private RecyclerView rv;
    private View view;
    private Button btn_add;
    private ImageView iv_box;
    private TextView tv_marked;
    private Context context;
    private ITuyaDevice mDevice;
    private RVAdapter rvAdapter;
    private String devId;

    public AddDeviceFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context =context;
        initMDevice();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDevice != null) {
            mDevice.unRegisterDevListener();
            mDevice.onDestroy();
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_add_device, container, false);
        context = inflater.getContext();

        initView();
        return view;
    }

    private void initView() {
        btn_add = view.findViewById(R.id.btn_add);
        iv_box = view.findViewById(R.id.iv_box);
        tv_marked = view.findViewById(R.id.tv_marked);
        btn_add.setOnClickListener(this);
        rv = view.findViewById(R.id.rv);

        if (mDevice != null) {
            Logger.d("mDevice != null");
            iv_box.setVisibility(View.GONE);
            tv_marked.setVisibility(View.GONE);
            btn_add.setVisibility(View.GONE);
            rv.setVisibility(View.VISIBLE);
            rv.setLayoutManager(new LinearLayoutManager(getContext()));
            rvAdapter = new RVAdapter();
            rv.setAdapter(rvAdapter);
        }else {
            rv.setVisibility(View.GONE);
            iv_box.setVisibility(View.VISIBLE);
            tv_marked.setVisibility(View.VISIBLE);
            btn_add.setVisibility(View.VISIBLE);
        }
    }

    private void initMDevice() {
        devId = MySharedPreferences.Build(context).getString("deviceId", null);
        Logger.i("devId: " + devId);
        if (devId != null) {
            //根据设备id初始化设备控制类
            mDevice = TuyaHomeSdk.newDeviceInstance(devId);
            /**
             * 设备对象。该设备的所有dp变化都会通过callback返回。
             *
             * 初始化设备之前，请确保已经初始化连接服务端，否则无法获取到服务端返回信息
             */
            mDevice.registerDevListener(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add:
                go2ChooseDeviceTypeActivity();
                break;
        }
    }

    private void go2ChooseDeviceTypeActivity() {
        startActivity(new Intent(getContext(), ChooseDeviceTypeActivity.class));
        getActivity().finish();
    }

    @Override
    public void onDpUpdate(String s, String s1) {

    }

    @Override
    public void onRemoved(String devId) {
        //设备被移除
        Logger.w("设备被移除\n" + "devId: " + devId);
        rvAdapter.setDevice_state(101);
        rv.removeViewAt(0);
        if (mDevice != null) {
            mDevice.unRegisterDevListener();
            mDevice.onDestroy();
            if (rv.getChildCount() == 0) {
                rv.setVisibility(View.GONE);
                iv_box.setVisibility(View.VISIBLE);
                tv_marked.setVisibility(View.VISIBLE);
                btn_add.setVisibility(View.VISIBLE);
            }

        }
    }

    @Override
    public void onStatusChanged(String s, boolean b) {

    }

    @Override
    public void onNetworkStatusChanged(String s, boolean b) {

    }

    @Override
    public void onDevInfoUpdate(String s) {

    }

    class RVAdapter extends RecyclerView.Adapter {

        private DeviceBean deviceBean;
        private int device_state;
        private Type7ViewHolder type7ViewHolder;

        @Override
        public int getItemViewType(int position) {
            return ITEM_VIEW_TYPE_7;
        }

        public int getDevice_state() {
            return device_state;
        }

        public void setDevice_state(int device_state) {
            this.device_state = device_state;
            switch (device_state){
                case 101:
                    type7ViewHolder.getDeviceState(101);
                    break;
            }
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            return new Type7ViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.device_three_gang_switch_type7_item, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
            type7ViewHolder = (Type7ViewHolder) viewHolder;
            type7ViewHolder.getPosition(position);


        }

        @Override
        public int getItemCount() {
            return 1;
        }
    }
}
